<template>
  <view class="container">
    <el-row style="height: 100vh">
      <el-col :span="12" :xs="0">
        <div class="login-left-wrap">
          <img
              class="logo"
              src="@/assets/images/common/logo.png"
              alt="佰茵云"
          />
          <img
              class="bg"
              src="@/assets/images/login/login-bg.png"
              alt="佰茵云"
          />
        </div>
      </el-col>
      <el-col :span="12" :xs="24" class="flex-xyc">
        <div class="form-right-wrap">
          <div class="form-wrap">
            <h1>欢迎来到佰茵云</h1>
            <h2>账号{{ isLoginPage ? '登录' : '注册' }}</h2>
            <el-form
                class="form"
                ref="fromRef"
                :model="formData"
                :rules="rules"
            >
              <el-form-item prop="username">
                <el-input
                    v-model="formData.username"
                    placeholder="请输入您的账号"
                />
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                    type="password"
                    v-model="formData.password"
                    placeholder="请输入密码"
                    show-password
                />
              </el-form-item>
              <el-form-item prop="copyPassword" v-if="!isLoginPage">
                <el-input
                    type="password"
                    v-model="formData.copyPassword"
                    placeholder="请确认密码"
                    show-password
                />
              </el-form-item>
              <el-form-item>
                <el-button class="btn" type="primary" @click="submitForm">
                  {{ isLoginPage ? '登录' : '注册' }}
                </el-button>
              </el-form-item>
            </el-form>
            <!--条款-->
            <div class="tk">
              <el-checkbox v-model="agree">同意佰茵云</el-checkbox>
              <router-link to="">《服务条款》</router-link>
              <router-link to="">《隐私政策》</router-link>
            </div>
            <div class="pass-find-login flex-yc flex-xbt">
              <router-link class="find-pass-btn" to="">忘记密码？</router-link>
              <div v-if="!isLoginPage" class="login-btn flex-yc">
                已有账号，
                <router-link to="/login">去登录</router-link>
              </div>
              <div v-else class="login-btn flex-yc">
                还没有账号，
                <router-link to="/reg">立即注册</router-link>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </view>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { reqLogin, reqRegister, getUserDetail } from '@/api/user.ts'
import { loginForm, regForm } from '@/api/types/user'
import { MsgError, MsgSuccess } from '@/utils/utils'
import { useUserStore } from '@/store/modules/user.ts'
const userStore = useUserStore()

let route = useRoute()
let router = useRouter()
const isLoginPage = computed(() => route.path === '/login')
let isLogin = userStore.userInfo.token
if (isLogin) router.push('/home')

const formData = reactive<regForm>({
  username: '',
  password: '',
  copyPassword: '',
})

const rules = reactive<FormRules<regForm>>({
  username: [{ required: true, message: '请输入您的账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  copyPassword: [
    { required: true, message: '请输入确认密码', trigger: 'blur' },
  ],
})

const fromRef = ref<FormInstance>()
const agree = ref(false)

const submitForm = () => {
  fromRef.value!.validate(async (valid: boolean) => {
    if (valid) {
      if (!agree.value) return MsgError('请勾选佰茵云服务条款和隐私条款')
      if (!isLoginPage.value) {
        if (formData.password !== formData.copyPassword) {
          return MsgError('两次密码输入不一致')
        }
        let resp = await reqRegister(formData)
        if (resp.code !== 0) return MsgError(resp.msg || '注册失败')
        MsgSuccess(resp.msg || '注册成功')
        router.push('/login')
      } else {
        let loginData: loginForm = {
          username: formData.username,
          password: formData.password,
        }
        let resp = await reqLogin(loginData)
        if (resp.code !== 0) return MsgError(resp.msg || '登录失败')
        userStore.setUserInfo(resp.data);
        let userInfo = await getUserDetail();
        console.log(userInfo, 'user')
        if (userInfo.code !== 200) return MsgError(userInfo.msg || '获取用户信息失败')
        userStore.setUserInfo({...resp.data, ...userInfo.data});
        MsgSuccess(resp.msg || '登录成功')
        setTimeout(() => {
          const curr = localStorage.getItem('preRoute')
          if (curr == null) {
            router.push('/home')
          } else {
            router.push(curr)
          }
        }, 1500)
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.container {
  .login-left-wrap {
    width: 100%;
    height: 100%;
    background-color: $primary;

    .logo {
      width: 149px;
      margin: 53px 38px;
    }

    .bg {
      width: 90%;
      margin: 0 auto;
    }
  }

  .form-right-wrap {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    height: 100%;

    .form-wrap {
      h1 {
        font-size: 32px;
        font-weight: 500;
        color: #021429;
      }

      > h2 {
        width: 80px;
        padding-bottom: 8px;
        margin-top: 47px;
        font-size: 16px;
        font-weight: 500;
        color: $primary;
        border-bottom: 2px solid $primary;
      }

      .form {
        width: 366px;
        margin-top: 22px;

        :deep(.el-form-item) {
          .el-input,
          .el-input__inner {
            height: 38px !important;
          }

          .el-form-item {
            margin-bottom: 26px;
          }
        }

        .btn {
          width: 100%;
          height: 40px;
          margin-top: 35px;
        }
      }

      .tk {
        display: flex;
        align-items: center;

        a {
          color: $primary;
        }
      }

      .pass-find-login {
        margin-top: 30px;
        font-size: 14px;

        .find-pass-btn {
          color: $danger;
        }

        .login-btn a {
          color: $primary;
        }
      }
    }
  }
}
</style>
